import { actionTypes } from './../actions/saga';

let initState = {
    sagaNewsType: null,
    sagaNewsList: null,
    sagaNewsDetail: null,
    newsTypeError: null,
    newsListError: null,
    newsDetailError: null,
    placeholderData: null
};

const reducer = (state = initState, action) => {
    switch (action.type) {
        case actionTypes.TYPE_REQUEST:
            return { ...state, isFetching: true };
        case actionTypes.TYPE_SUCCESS:
            return {
                ...state,
                sagaNewsType: action.payload,
                isFetching: false
            };
        case actionTypes.TYPE_ERROR:
            return {
                ...state,
                isFetching: false,
                newsTypeError: action.error
            };
        case actionTypes.LIST_REQUEST:
            return { ...state, isFetching: true };
        case actionTypes.LIST_SUCCESS:
            state.topicLists = [];
            return {
                ...state,
                sagaNewsList: action.payload,
                isFetching: false
            };
        case actionTypes.LIST_ERROR:
            return {
                ...state,
                isFetching: false,
                newsListError: action.error
            };
        case actionTypes.DETAIL_REQUEST:
            return { ...state, isFetching: true };
        case actionTypes.DETAIL_SUCCESS:
            state.topicLists = [];
            return {
                ...state,
                sagaNewsDetail: action.payload,
                isFetching: false
            };
        case actionTypes.DETAIL_ERROR:
            return {
                ...state,
                isFetching: false,
                newsDetailError: action.error
            };
        case actionTypes.LOAD_DATA_SUCCESS:
            return {
                ...state,
                ...{ placeholderData: action.data }
            };
        default:
            return state;
    }
};

export default reducer;
